/**
 * Package:cn.kmpro.security;
 * $Id$
 * Copyright(c) 2001-2014 www.kmpro.cn
 */
package com.yigang.config;

import com.yigang.entity.User;
import com.yigang.utils.UserConst;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 认证检查授权拦截器.
 * <p/>检查请求是否登录过,是认证的请求
 *
 * @version 1.0
 */
@Component
public class AuthenticationInterceptor implements HandlerInterceptor {
    private static Log logger = LogFactory.getLog(AuthenticationInterceptor.class);


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        String uri = request.getRequestURI();
        String method = request.getMethod();
        logger.debug("now uri:" + uri + " method:" + method);
        Object attribute = request.getSession().getAttribute(UserConst.LOGIN_USER_INFO);
        if (null == attribute) {
            logger.error("CODE_AUTHENTICATION_ERROR for uri:" + uri + ",method" + method);
            response.sendRedirect(request.getContextPath() + "/jsp/userLogin.jsp");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception {

    }
}
